<?php
/**
 * BRail_Application_Resource_Log
 *
 * This class creates a new Instance of Zend_Log,
 * that will write into the directory defined by resource.log.path option
 * in application.ini, replacing any %d found by date('Y-m-d') creating a log rotation by day.
 * <strong>That means that you should ask for a deletion of old log files</strong>
 * If resource.log.firebug is defined, it will also write it
 * in the Firebug console (you don't want that in production).
 */
class BRail_Application_Resource_Log extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * Initialize the Zend_Log object, configuring it to write in resource.
     *
     * @return void
     */
    public function init()
    {
        if (Zend_Registry::isRegistered('Zend_Log')) {
            return Zend_Registry::get('Zend_Log');
        }
        $options = $this->getOptions();
        $filename = str_replace('%d', date('Y-m-d'), $options['path']);
        $log = new Zend_Log();
        $log->addWriter(new Zend_Log_Writer_Stream($filename));
        if (isset($options['firebug']) && $options['firebug'] == 1) {
            $log->addWriter(new Zend_Log_Writer_Firebug());
        }
        Zend_Registry::set('Zend_Log', $log);
        return $log;
    }
}